/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.GroupRule_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class GroupRule_DAO {
    
     public static String Get_GroupID() throws SQLException{
        String GroupID = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "FD");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            GroupID = Rs.getString(1);
        }
        return GroupID;
    }
    
    public static ArrayList<GroupRule_DTO> GetALL()
    {
        ArrayList<GroupRule_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from DoanTau" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                GroupRule_DTO Func = new GroupRule_DTO();
                Func.setGroupID(rs.getString("GroupID"));
                Func.setGroupName(rs.getString("GroupName"));
                Func.setDienGiai(rs.getString("DienGiai"));
                Func.setActive(rs.getBoolean("Active"));
                Func.setMenuID(rs.getString("MenuID"));
                ds.add(Func);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(GroupRule_DTO GroupRule)
    {
        boolean flag = false;
        try {
            String sql = "Insert GroupRule Values('"+GroupRule.getGroupID()+"','"+GroupRule.getGroupName()+"','"+GroupRule.getDienGiai()+"','"+GroupRule.isActive()+"','"+GroupRule.getMenuID()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(GroupRule_DTO GroupRule)
    {
        boolean flag = false;
        try {
            String sql = "Update GroupRule Set GroupName = '"+ GroupRule.getGroupName() +"', DienGiai = '"+GroupRule.getDienGiai()+"', Active = "+GroupRule.isActive()+", MenuID = '"+ GroupRule.getMenuID()+ "' where GroupID = '"+ GroupRule.getGroupID()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String _GroupID)
    {
        boolean flag = false;
        try {
            String sql = "Delete from GroupRule where GroupID = '"+_GroupID+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
